package com.karaoke.android.home.widget

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.media.MediaPlayer
import android.view.View
import com.karaoke.android.home.R
import com.labo.kaji.relativepopupwindow.RelativePopupWindow


/**
 * Created by allen on 2017/4/30.
 */

class AuraPopup(context: Context) : RelativePopupWindow(context)
{
    private val mediaPlayer = MediaPlayer()
    private val asset = context.assets

    private val onClickListener: View.OnClickListener = View.OnClickListener {
        mediaPlayer.stop()
        mediaPlayer.reset()
        when (it.id)
        {
            R.id.btnHuanhu  ->
            {
                val fileDescriptor = asset.openFd("huanhu.mp3")
                mediaPlayer.setDataSource(fileDescriptor.fileDescriptor, fileDescriptor.startOffset,
                                          fileDescriptor.startOffset)
            }
            R.id.btnKouSao  ->
            {
                val fileDescriptor = asset.openFd("koushao.mp3")
                mediaPlayer.setDataSource(fileDescriptor.fileDescriptor, fileDescriptor.startOffset,
                                          fileDescriptor.startOffset)
            }
            R.id.btnGuZhang ->
            {
                val fileDescriptor = asset.openFd("guzhang.mp3")
                mediaPlayer.setDataSource(fileDescriptor.fileDescriptor, fileDescriptor.startOffset,
                                          fileDescriptor.startOffset)
            }
            R.id.btnDaoCai  ->
            {
                val fileDescriptor = asset.openFd("daocai.mp3")
                mediaPlayer.setDataSource(fileDescriptor.fileDescriptor, fileDescriptor.startOffset,
                                          fileDescriptor.startOffset)
            }
        }
        mediaPlayer.prepare()
        mediaPlayer.start()
        this.dismiss()
    }

    init
    {
        contentView = View.inflate(context, R.layout.aura_dialog, null)
        contentView.findViewById(R.id.btnHuanhu).setOnClickListener(onClickListener)
        contentView.findViewById(R.id.btnKouSao).setOnClickListener(onClickListener)
        contentView.findViewById(R.id.btnGuZhang).setOnClickListener(onClickListener)
        contentView.findViewById(R.id.btnDaoCai).setOnClickListener(onClickListener)
        width = 344
        height = 148
        isFocusable = false
        isOutsideTouchable = true
        setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        contentView.measure(344, 148)
    }

    override fun showOnAnchor(anchor: View, vertPos: Int, horizPos: Int)
    {
        super.showOnAnchor(anchor, vertPos, horizPos)
    }
}
